package com.gxa.controller.points;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.Result;
import com.gxa.dto.pointsdto.IntegralDto;
import com.gxa.dto.pointsdto.UserDto;
import com.gxa.entity.points.Integral;
import com.gxa.entity.points.Integration;
import com.gxa.service.points.IntegralService;
import com.gxa.service.points.IntegrationService;
import com.gxa.vo.IntegralVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-12 11:43:53
 */
@Api(tags = "积分明细模块")
@RestController
@RequestMapping("/integral")
public class IntegralController {
    @Autowired
    private IntegralService integralService;


    @Autowired
    private IntegrationService integrationService;

    @ApiOperation(value = "积分明细列表查询")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @PostMapping("/integrals")
    public Result<PageInfo> queryAll(@RequestParam("pagenum") Integer pagenum,@RequestParam("count") Integer count){
        try{
        PageInfo<IntegralDto> integrals = this.integralService.queyAll(pagenum, count);
            Result<PageInfo> r = Result.success(integrals);
            return r;
        }catch (Exception e){
            e.printStackTrace();
            Result r = Result.failed("查询失败");
            return r;
        }
    }

    @ApiOperation(value = "根据id查积分明细")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @GetMapping("/integral/{id}")
    public Result<IntegralDto> queryById(@PathVariable("id") Integer id){
            try {
                IntegralDto i = this.integralService.queryById(id);
                return Result.success(i);
            }catch (Exception e){
                e.printStackTrace();
                return Result.failed("查找失败");
            }

    }


    @ApiOperation(value = "删除积分明细数据")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @DeleteMapping("/integral/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        try {
            this.integralService.delete(id);
            return Result.success("更新成功");
        } catch (Exception e) {
            return Result.failed("更新失败");
        }
    }

    @ApiOperation(value = "积分条件查询")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @PostMapping("/integral")
    public Result queryByVo(@RequestBody IntegralVo integralVo) {
        System.out.println(integralVo);
        try {
            PageInfo<IntegralDto> integrals = this.integralService.queryByVo(integralVo);
                Result<PageInfo> r = Result.success(integrals);
                return r;
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("查询失败");
        }
    }

    @ApiOperation(value = "用户名字")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @PostMapping("/userlist")
    public Result queryAll() {
        List<UserDto> userDtos = this.integrationService.queryAll();
        return Result.success(userDtos);
    }

}
